/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

using EnvDTE;

using DslEditorPowerToy.Recipes.ValueProviders;
using DslEditorPowerToy.VisualStudio;

namespace DslEditorPowerToy.Recipes.Configuration.ValueProviders
{
    /// <summary>
    /// Returns the existing project item, using value from project globals
    /// </summary>
    public class ProjectItemFromProjectGlobalsValueProvider : ValueProviderBase
    {
        private string _projectArgumentName = string.Empty;
        private string _propertyName = string.Empty;
        private DteHelper.ProjectItemType _itemType = DteHelper.ProjectItemType.File;

        #region Constructors
        public ProjectItemFromProjectGlobalsValueProvider()
            : base()
        {
        }
        #endregion

        #region Public Methods
        public override void Configure(StringDictionary attributes)
        {
            if (false == attributes.ContainsKey("ProjectArgument"))
                throw new ArgumentNullException("ProjectArgument");
            this._projectArgumentName = attributes["ProjectArgument"];
            if (false == attributes.ContainsKey("PropertyName"))
                throw new ArgumentNullException("PropertyName");
            this._propertyName = attributes["PropertyName"];
            if (true == attributes.ContainsKey("ItemType"))
                this._itemType = (DteHelper.ProjectItemType)Enum.Parse(typeof(DteHelper.ProjectItemType), attributes["ItemType"], true);
        }
        public override bool OnArgumentChanged(string changedArgumentName, object changedArgumentValue, object currentValue, out object newValue)
        {
            //Check the current value
            if (null == currentValue)
            {
                //Return value from globals
                string defaultValue = GetValueFromGlobals();
                if (null == defaultValue)
                {
                    newValue = null;
                    return false;
                }

                //Try default value
                newValue = GetValue(defaultValue);
                return true;
            }

            //Try changed value
            newValue = GetValue((string)currentValue);
            return true;
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Returns a verified project item path
        /// </summary>
        /// <returns></returns>
        private string GetValue(string testValue)
        {
            //Dereference project argument
            Project project = (Project)this.GetArgumentValue(this._projectArgumentName);
            if (null == project)
                return null;

            //Get project item from project
            ProjectItem fileItem = DteHelper.FindProjectItemByName(project, testValue, this._itemType);
            if (null != fileItem)
                return testValue;
            
            return null;
        }
        /// <summary>
        /// Returns the value from the project globals collection
        /// </summary>
        /// <returns></returns>
        private string GetValueFromGlobals()
        {
            //Dereference project argument
            Project project = (Project)this.GetArgumentValue(this._projectArgumentName);
            if (null == project)
                return null;

            //Ensure vlaue exists
            if (false == DteHelper.IsGlobalsVariableExists(project.Globals, this._propertyName))
                return null;

            return DteHelper.GetVariableFromGlobals(project.Globals, this._propertyName);
        }
        #endregion
    }
}
